Return to start page

Core/Environment/Struct Dynamic Lightning.j

Code

		
1			library AStructCoreEnvironmentDynamicLightning requires optional ALibraryCoreDebugMisc, ALibraryCoreEnvironmentLightning, AStructCoreGeneralVector, ALibraryCoreMathsHandle

2
3 /**
4 * Dynamic lightnings are lightnings which are moved automatically between positions of to specific units.
5 */
6 struct ADynamicLightning
7 //static start members
8 private static boolean removeOnDeath
9 //static members
10 private static timer moveTimer
11 private static AIntegerVector dynamicLightnings
12 //start members
13 private player m_player
14 private string m_code
15 //dynamic members
16 private unit m_firstUnit
17 private unit m_secondUnit
18 //members
19 private integer m_index
20 private lightning m_lightning
21
22 //! runtextmacro optional A_STRUCT_DEBUG("\"ADynamicLightning\"")
23
24 //start members
25
26 public method player takes nothing returns player
27 return this.m_player
28 endmethod
29
30 public method code takes nothing returns string
31 return this.m_code
32 endmethod
33
34 //dynamic members
35
36 public method setFirstUnit takes unit firstUnit returns nothing
37 set this.m_firstUnit = firstUnit
38 endmethod
39
40 public method firstUnit takes nothing returns unit
41 return this.m_firstUnit
42 endmethod
43
44 public method setSecondUnit takes unit secondUnit returns nothing
45 set this.m_secondUnit = secondUnit
46 endmethod
47
48 public method secondUnit takes nothing returns unit
49 return this.m_secondUnit
50 endmethod
51
52 //methods
53
54 public method setLightningColor takes real red, real green, real blue, real alpha returns boolean
55 return SetLightningColor(this.m_lightning, red, green, blue, alpha)
56 endmethod
57
58 public method lightningColorRed takes nothing returns real
59 return GetLightningColorR(this.m_lightning)
60 endmethod
61
62 public method lightningColorGreen takes nothing returns real
63 return GetLightningColorG(this.m_lightning)
64 endmethod
65
66 public method lightningColorBlue takes nothing returns real
67 return GetLightningColorB(this.m_lightning)
68 endmethod
69
70 public method lightningColorAlpha takes nothing returns real
71 return GetLightningColorA(this.m_lightning)
72 endmethod
73
74 private method refreshPosition takes nothing returns nothing
75 call MoveLightningEx(this.m_lightning, false, GetUnitX(this.m_firstUnit), GetUnitY(this.m_firstUnit), GetUnitZ(this.m_firstUnit), GetUnitX(this.m_secondUnit), GetUnitY(this.m_secondUnit), GetUnitZ(this.m_secondUnit))
76 endmethod
77
78 private method createLightning takes nothing returns nothing
79 if (this.m_player == null) then
80 set this.m_lightning = AddLightningEx(this.m_code, false, GetUnitX(this.m_firstUnit), GetUnitY(this.m_firstUnit), GetUnitZ(this.m_firstUnit), GetUnitX(this.m_secondUnit), GetUnitY(this.m_secondUnit), GetUnitZ(this.m_secondUnit))
81 else
82 set this.m_lightning = CreateLightningForPlayer(this.m_player, this.m_code, GetUnitX(this.m_firstUnit), GetUnitY(this.m_firstUnit), GetUnitZ(this.m_firstUnit), GetUnitX(this.m_secondUnit), GetUnitY(this.m_secondUnit), GetUnitZ(this.m_secondUnit))
83 endif
84 endmethod
85
86 /**
87 * @param user If user is null, dynamic lightning will be visible for all players.
88 */
89 public static method create takes player user, string usedCode, unit firstUnit, unit secondUnit returns thistype
90 local thistype this = thistype.allocate()
91 //start members
92 set this.m_player = user
93 set this.m_code = usedCode
94 //dynamic members
95 set this.m_firstUnit = firstUnit
96 set this.m_secondUnit = secondUnit
97 //members
98 call thistype.dynamicLightnings.pushBack(this)
99 set this.m_index = thistype.dynamicLightnings.backIndex()
100 call this.createLightning()
101
102 return this
103 endmethod
104
105 private method destroyLightning takes nothing returns nothing
106 call DestroyLightning(this.m_lightning)
107 set this.m_lightning = null
108 endmethod
109
110 public method onDestroy takes nothing returns nothing
111 //static members
112 call thistype.dynamicLightnings.erase(this.m_index)
113 //start members
114 set this.m_player = null
115 //dynamic members
116 set this.m_firstUnit = null
117 set this.m_secondUnit = null
118 //members
119 call this.destroyLightning()
120 endmethod
121
122 private static method timerFunction takes nothing returns nothing
123 local thistype dynamicLightning
124 local integer i = 0
125 loop
126 exitwhen (i == thistype.dynamicLightnings.size())
127 set dynamicLightning = thistype.dynamicLightnings[i]
128 if (thistype.removeOnDeath and (IsUnitDeadBJ(dynamicLightning.m_firstUnit) or IsUnitDeadBJ(dynamicLightning.m_secondUnit))) then
129 call dynamicLightning.destroy()
130 else
131 call dynamicLightning.refreshPosition()
132 set i = i + 1
133 endif
134 endloop
135 endmethod
136
137 public static method init takes real refreshRate, boolean removeOnDeath returns nothing
138 //static start members
139 set thistype.removeOnDeath = removeOnDeath
140 //static members
141 set thistype.moveTimer = CreateTimer()
142 call TimerStart(thistype.moveTimer, refreshRate, true, function thistype.timerFunction)
143 set thistype.dynamicLightnings = AIntegerVector.create()
144 endmethod
145
146 public static method cleanUp takes nothing returns nothing
147 //static members
148 call PauseTimer(thistype.moveTimer)
149 call DestroyTimer(thistype.moveTimer)
150 set thistype.moveTimer = null
151 loop
152 exitwhen (thistype.dynamicLightnings.empty())
153 call thistype(thistype.dynamicLightnings.back()).destroy()
154 endloop
155 call thistype.dynamicLightnings.destroy()
156 endmethod
157 endstruct
158
159 endlibrary